"""
使用一个邮箱向另一个邮箱发送测试报告的html文件，这里需要对发送邮件的邮箱进行设置，获取邮箱授权码。
username=“发送邮件的邮箱”， password=“邮箱授权码”
这里要特别注意password不是邮箱密码而是邮箱授权码。
mail_server = "发送邮箱的服务器地址"
这里常用的有 qq邮箱——"stmp.qq.com", 163邮箱——"stmp.163.com"
其他邮箱服务器地址可自行百度
"""
import os
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time

# 邮件信息在readme.md
class SendEmail():
    def send_email(self, asin, followAsin):
        try:
            mail_body = '''<br>这是你的卖点链接:&nbsp;&nbsp;<a style="color:red;" href="https://www.amazon.com/dp/%s" traget="_blank">https://www.amazon.com/dp/%s</a><br>
            <br>这是你的查看跟卖的总数链接:(可能不止一个人跟卖你)&nbsp;&nbsp;<a style="color:red;" href="https://www.amazon.com/gp/offer-listing/%s" traget="_blank">https://www.amazon.com/gp/offer-listing/%s</a><br>
            <br>这是跟卖你的点:&nbsp;&nbsp;<b style="color:red;">%s<b>
            <br><br>
            <p>温馨提示:  在群里通知一下小伙伴吧!</p>
            <br>加油!!!  辛苦了''' % (asin, asin, asin, asin, followAsin)
            # 发件人地址
            send_addr = 'FollowSale@163.com'
            # 收件人地址
            reciver_addr = 'FollowSale@163.com'
            # reciver_addr = 'jwlxzysjd@126.com'
            # 发送邮箱的服务器地址 qq邮箱是'smtp.qq.com', 163邮箱是'smtp.163.com'
            mail_server = 'smtp.163.com'
            now = time.strftime("%Y-%m-%d  %H:%M:%S")
            # 邮件标题
            # subject = '谁在跟卖我们'
            subject = '%s 被===> %s 跟卖了!!!!  %s' % (asin, followAsin, now)
            # 发件人的邮箱及邮箱授权码
            username = 'FollowSale@163.com'
            password = 'zhudepanshi153'  # 注意这里是邮箱的授权码而不是邮箱密码
            # 邮箱的内容和标题
            message = MIMEText(mail_body, 'html', 'utf-8')
            message['Subject'] = Header(subject, charset='utf8')
            message['From'] = 'KB' + '<FollowSale@163.com>'
            message['To'] = 'FollowSale@163.com'
            # message['To'] = 'jwlxzysjd@126.com'

            # 发送邮件，使用的使smtp协议
            smtp = smtplib.SMTP()
            smtp.connect(mail_server)
            smtp.login(username, password)
            # smtp.sendmail(send_addr, reciver_addr, message.as_string())
            smtp.sendmail(send_addr, ['FollowSale@163.com'], message.as_string())
            print('已发送邮件')
            smtp.quit()
        except IndexError as e:
            print(e)


# if __name__ == '__main__':
#     SendEmail().send_email('sdfasfre', 'dsfefrefffff')
